﻿//======================================================================
//  文件上传帮助
//====================================================================== 

namespace Common
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Drawing.Imaging;
    using System.Drawing;
    using System.Drawing.Drawing2D;



    /// <summary>
    /// 在此描述UploadHelp的说明
    /// </summary>
    public class UploadHelp
    {
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="oldpath">原图片地址</param>
        /// <param name="newpath">新图片地址</param>
        /// <param name="tWidth">缩略图的宽</param>
        /// <param name="tHeight">缩略图的高</param>
        public void GreateMiniImage(string oldpath, string newpath, int tWidth, int tHeight)
        {

            try
            {

                System.Drawing.Image image = System.Drawing.Image.FromFile(oldpath);
                double bl = 1d;
                if ((image.Width <= image.Height) && (tWidth >= tHeight))
                {
                    bl = Convert.ToDouble(image.Height) / Convert.ToDouble(tHeight);
                }
                else if ((image.Width > image.Height) && (tWidth < tHeight))
                {
                    bl = Convert.ToDouble(image.Width) / Convert.ToDouble(tWidth);

                }
                else

                    if ((image.Width <= image.Height) && (tWidth <= tHeight))
                    {
                        if (image.Height / tHeight >= image.Width / tWidth)
                        {
                            bl = Convert.ToDouble(image.Width) / Convert.ToDouble(tWidth);

                        }
                        else
                        {
                            bl = Convert.ToDouble(image.Height) / Convert.ToDouble(tHeight);
                        }
                    }
                    else
                    {
                        if (image.Height / tHeight >= image.Width / tWidth)
                        {
                            bl = Convert.ToDouble(image.Height) / Convert.ToDouble(tHeight);

                        }
                        else
                        {
                            bl = Convert.ToDouble(image.Width) / Convert.ToDouble(tWidth);

                        }

                    }
                System.Drawing.Bitmap b = new System.Drawing.Bitmap(image, Convert.ToInt32(image.Width / bl), Convert.ToInt32(image.Height / bl));
                b.Save(newpath);
                b.Dispose();
                image.Dispose();
            }
            catch
            {
            }

        }
    }
}
